package edu.wong.pro.serviceImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.sun.mail.handlers.message_rfc822;

import edu.wong.pro.dao.MemberDAO;
import edu.wong.pro.iservice.IMember;
import edu.wong.pro.model.Member;
import edu.wong.pro.model.Statistics;
import edu.wong.pro.other.MD5;

public class MemberService implements IMember{

	@Override
	public void addMember(String name,String number) {
		// TODO Auto-generated method stub
		MemberDAO dao = new MemberDAO();
		Member member = new Member();
		member.setMemname(name);
		member.setNumber(number);
		member.setPassword("888888");
		member.setActive(0);
		member.setContribution(0);
		//密码加密
		//member.setPassword(MD5.getMd5(member.getPassword()));
		Session session = dao.getSession();
		Transaction transaction = session.beginTransaction();
		//保存用户
		dao.save(member);	
		transaction.commit();
		session.close();
		//System.out.println("id="+member.getMemId());
	}

	
	@Override
	public Member findMemberByID(String memID) {
		// TODO Auto-generated method stub
		Member member;
		MemberDAO dao = new MemberDAO();
		member = dao.findById(Integer.parseInt(memID.trim()));
		return member;
	}

	//完善个人信息
	@Override
	public void improveMember(String number, String password, String name,
			String sex, String borth, String homeaddr, String indate,
			String shschool, String college, String major, String schooladdr,
			String phone, String cornet, String email, String workaddr,
			String worktype, String position, String detail) {
			// TODO Auto-generated method stub
			//
			ServletActionContext.getRequest().getSession().removeAttribute("isLogin");
			int memID = Integer.parseInt(ServletActionContext.getRequest().getSession().getAttribute("memberID").toString());
			MemberDAO dao = new MemberDAO();
			Member member = dao.findById(memID);
			member.setNumber(number);
			member.setPassword(password);
			member.setMemname(name);
			member.setGender(sex);
			member.setBorthday(borth);
			member.setHomeAddr(homeaddr);
			member.setIndate(indate);
			member.setShschool(shschool);
			member.setCollege(college);
			member.setMajor(major);
			member.setSchooladdr(schooladdr);
			member.setPhone(phone);
			member.setCornet(cornet);
			member.setEmail(email);
			member.setWorkaddr(workaddr);
			member.setWorktype(worktype);
			member.setPosition(position);
			member.setDetail(detail);
			Session session = dao.getSession();
			Transaction transaction = session.beginTransaction();
			dao.attachClean(member);
			transaction.commit();
			session.close();
			
	}


	@Override
	public void modifyMember(String number, String name, String sex,
			String borth, String homeaddr, String indate, String shschool,
			String college, String major, String schooladdr, String phone,
			String cornet, String email, String workaddr, String worktype,
			String position, String detail) {
		// TODO Auto-generated method stub
		int memID = Integer.parseInt(ServletActionContext.getRequest().getSession().getAttribute("memberID").toString());
		MemberDAO dao = new MemberDAO();
		Member member = dao.findById(memID);
		member.setNumber(number);
		member.setMemname(name);
		member.setGender(sex);
		member.setBorthday(borth);
		member.setHomeAddr(homeaddr);
		member.setIndate(indate);
		member.setShschool(shschool);
		member.setCollege(college);
		member.setMajor(major);
		member.setSchooladdr(schooladdr);
		member.setPhone(phone);
		member.setCornet(cornet);
		member.setEmail(email);
		member.setWorkaddr(workaddr);
		member.setWorktype(worktype);
		member.setPosition(position);
		member.setDetail(detail);
		Session session = dao.getSession();
		Transaction transaction = session.beginTransaction();
		dao.attachClean(member);
		transaction.commit();
		session.close();
		
	}


	//同乡录和查询
	@Override
	public List<Member> getAllMember(String name,String indate,String college,String major) {
		// TODO Auto-generated method stub
		List<Member> list;
		MemberDAO dao = new MemberDAO();
		Member member = new Member();
		if(name==null&&indate==null&&college==null&&major==null){
			list =dao.findAll();
		}else{
			if(!name.equals("")){
				member.setMemname(name.trim());
			}
			if(!indate.equals("")){
				member.setIndate(indate.trim());
			}
			if(!college.equals("")){
				member.setCollege(college.trim());
			}
			if(!major.equals("")){
				member.setMajor(major.trim());
			}
			
			list = dao.findByExample(member);
		}
		
		return list;
	}


	@Override
	public JSONArray getStatistics(String flag) {
		// TODO Auto-generated method stub
		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		Query query = session.createQuery("select "+flag+",count(*) from Member group by "+flag);
		List list = query.list();
		Iterator iterator = list.iterator();
		JSONArray jsonArray = new JSONArray();
		ArrayList<JSONObject> jList = new ArrayList<JSONObject>();
		while(iterator.hasNext()){
		   Object[] objects = (Object[]) iterator.next();
		   System.out.println(objects[0]+","+objects[1]);
		   JSONObject jObject = new JSONObject();
		   jObject.put("value", objects[1]);
		   jObject.put("label", objects[0]);
		   jList.add(jObject);
	   }	
		return JSONArray.fromObject(jList);
	}

}
